package com.example.demo.mylove.service;

import com.example.demo.mylove.bean.MyLoveBean;
import com.example.demo.mylove.repository.MyLoveRepository;
import com.example.demo.service.BaseService;
import com.example.demo.shops.bean.ShopBean;
import com.example.demo.shops.service.ShopService;
import com.example.demo.user.bean.UserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyLoveService extends BaseService<MyLoveBean,MyLoveRepository>{

    @Autowired
    ShopService shopService;

    public void save(long id, UserBean userBean) {
//        return super.save(myLoveBean);

        if(repository.findAllByUserBean(userBean).parallelStream().noneMatch(b->
             b.getShopBean().getId()==id))
        {
            MyLoveBean myLoveBean = new MyLoveBean();
            myLoveBean.setUserBean(userBean);
            myLoveBean.setShopBean(shopService.getById((int) id));
            save(myLoveBean);
        }
        return ;
    }

    public List<ShopBean> getAllByUser(UserBean userBean) {
      return   repository.findAllByUserBean(userBean).parallelStream().map(b->b.getShopBean()).collect(Collectors.toList());
    }

    public void dellove(String id, UserBean userBean) {
        MyLoveBean myLoveBean = getById(Integer.valueOf(id));
        if (myLoveBean!=null&&myLoveBean.getUserBean()==userBean){
            delEntity(Integer.valueOf(id));
        }
    }
}
